﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Data.Linq;

namespace SupplyChainWebE9
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        private log4net.ILog log = log4net.LogManager.GetLogger("WebService1");

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        /// <summary>
        /// 未启用
        /// 对比阿里云服务器供应商可用建议和E9一致，将阿里云上不一致的建议删除
        /// </summary>
        /// <param name="vendorID"></param>
        [WebMethod(Description = "对比阿里云服务器供应商可用建议和E9一致，将阿里云上不一致的建议删除，未启用")]
        public void checkPoSug(string vendorID)
        {
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            string whereposugdtl = String.Format("VendorID = '{0}'", vendorID);
            bool morepage;
            dsposug = posugBO.GetRows(whereposugdtl, "", 0, 0, out morepage);
            //dsposug.Copy();

            var rows = dsposug.SugPoDtl.AsEnumerable().
                                            Select(p => new
                                            {
                                                SugNum = p.Field<int>("SugNum")
                                            }).Cast<int>();
            var obj =  SCMDataProvider.GetSugPoDtl(vendorID).Cast<SugPoDtl>().Where(s => !rows.Contains(s.SugNum) );
            foreach (var item in obj)
            {
                SCMDataProvider.DeleteSugPoDtl(item.SugNum);
            }
            //更新

        }

        /// <summary>
        /// 根据建议编号生成供应商采购单  （可多条建议合并处理）
        /// </summary>
        /// <param name="vendorID"></param>
        /// <param name="sugNums"></param>
        [WebMethod(Description = "根据建议编号生成供应商采购单  （可多条建议合并处理）")]
        public string  createPOBySugNum2(string vendorID, string sugNums)
        {
            string errSugNums = "";
            string[] nums = sugNums.Split(',');
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            //Int32 sugnum = sugNum;//2476043;
            //dsposug = posugBO.GetByID(sugnum);

            string whereClause = string.Format("VendorID ='{0}'", vendorID);
            bool morePages ;
            dsposug = posugBO.GetRows(whereClause, "", 0,0,out morePages);

            foreach (string sugnum in nums)
            {
                //dsposug.Tables["SugPoDtl"]dsposug.Tables["SugPoDtl"]dsposug.Tables["SugPoDtl"]
                if (dsposug.SugPoDtl.Select("SugNum = " + sugnum).Count() > 0)
                {

                }
                else
                {
                    //在e9中找不到建议号 标记已处理 no order
                    SCMDataProvider.UpdateCheckBox05(Int32.Parse(sugnum));
                    SCMDataProvider.UpdateCheckBox04(Int32.Parse(sugnum));
                    errSugNums += (sugnum+",");
                    log.InfoFormat("在e9中找不到建议号 标记已处理 vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }
            if (errSugNums.Length>0)
            {
                errSugNums = errSugNums.Substring(0, errSugNums.Length - 1);
                if (errSugNums.Length== sugNums.Length)
                {
                    return errSugNums;
                }
            }
            foreach (Epicor.Mfg.BO.POSuggDataSet.SugPoDtlRow r in dsposug.SugPoDtl)
            {
                string sugnum = r["SugNum"].ToString();
                if (nums.Contains(sugnum))
                {
                    
                    r["CommentText"] = Convert.ToString(r["CommentText"]) + String.Format("/SugNum:{0}/", sugnum);
                    //r["Buy"] = true;//              / SugNum:123456/
                    log.InfoFormat("更新vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }

            string buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            posugBO.Update(dsposug);
            foreach (Epicor.Mfg.BO.POSuggDataSet.SugPoDtlRow r in dsposug.SugPoDtl)
            {
                string sugnum = r["SugNum"].ToString();
                if (nums.Contains(sugnum))
                {

                    //r["CommentText"] = Convert.ToString(r["CommentText"]) + String.Format("/SugNum:{0}/", sugnum);
                    r["Buy"] = true;//              / SugNum:123456/
                    //log.InfoFormat("更新vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }

            buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            posugBO.Update(dsposug);
            DateTime? ipCutOffDate1 = DateTime.Now;
            posugBO.Generate(dsposug, "", buyrid, ipCutOffDate1);
            SCMDataProvider.UpdateCheckBox05(nums);
            ////System.Windows.Forms.MessageBox.Show(dsposug.Tables[0].Rows.Count.ToString());
            ////string supperid = vendorID;//"GC04";
            //string buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            //dsposug.Tables[0].Rows[0]["Buy"] = true;
            //posugBO.Update(dsposug);
            //DateTime ? ipCutOffDate1 = DateTime.Now;
            //posugBO.Generate(dsposug, "", buyrid, ipCutOffDate1);
            //SCMDataProvider.UpdateCheckBox05(sugnum);
            log.InfoFormat("创建采购单订单成功，vendorID={0}; sugNums={1}", vendorID, sugNums);
            return errSugNums;
        }

        /// <summary>
        /// 根据建议编号生成供应商采购单  （可多条建议合并处理）
        /// </summary>
        /// <param name="vendorID"></param>
        /// <param name="sugNums"></param>
        [WebMethod(Description = "根据建议编号生成供应商采购单  （可多条建议合并处理）")]
        public void createPOBySugNum(string vendorID, string sugNums)
        {
            
            string[] nums = sugNums.Split(',');
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            //Int32 sugnum = sugNum;//2476043;
            //dsposug = posugBO.GetByID(sugnum);

            string whereClause = string.Format("VendorID ='{0}'", vendorID);
            bool morePages;
            dsposug = posugBO.GetRows(whereClause, "", 0, 0, out morePages);

            foreach (string sugnum in nums)
            {
                if (dsposug.Tables[0].Select("SugNum = " + sugnum).Count() > 0)
                {

                }
                else
                {
                    //在e9中找不到建议号 标记已处理 no order
                    SCMDataProvider.UpdateCheckBox05(Int32.Parse(sugnum));
                    
                    log.InfoFormat("在e9中找不到建议号 标记已处理 vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }
            
            foreach (Epicor.Mfg.BO.POSuggDataSet.SugPoDtlRow r in dsposug.SugPoDtl)
            {
                string sugnum = r["SugNum"].ToString();
                if (nums.Contains(sugnum))
                {

                    r["CommentText"] = Convert.ToString(r["CommentText"]) + String.Format("/SugNum:{0}/", sugnum);
                    //r["Buy"] = true;//              / SugNum:123456/
                    log.InfoFormat("更新vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }

            string buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            posugBO.Update(dsposug);
            foreach (Epicor.Mfg.BO.POSuggDataSet.SugPoDtlRow r in dsposug.SugPoDtl)
            {
                string sugnum = r["SugNum"].ToString();
                if (nums.Contains(sugnum))
                {

                    //r["CommentText"] = Convert.ToString(r["CommentText"]) + String.Format("/SugNum:{0}/", sugnum);
                    r["Buy"] = true;//              / SugNum:123456/
                    //log.InfoFormat("更新vendorID={0}; sugNum={1}已购买字段", vendorID, sugnum);
                }

            }

            buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            posugBO.Update(dsposug);
            DateTime? ipCutOffDate1 = DateTime.Now;
            posugBO.Generate(dsposug, "", buyrid, ipCutOffDate1);
            SCMDataProvider.UpdateCheckBox05(nums);
            ////System.Windows.Forms.MessageBox.Show(dsposug.Tables[0].Rows.Count.ToString());
            ////string supperid = vendorID;//"GC04";
            //string buyrid = dsposug.Tables[0].Rows[0]["BuyerID"].ToString();
            //dsposug.Tables[0].Rows[0]["Buy"] = true;
            //posugBO.Update(dsposug);
            //DateTime ? ipCutOffDate1 = DateTime.Now;
            //posugBO.Generate(dsposug, "", buyrid, ipCutOffDate1);
            //SCMDataProvider.UpdateCheckBox05(sugnum);
            log.InfoFormat("创建采购单订单成功，vendorID={0}; sugNums={1}", vendorID, sugNums);
            
        }

        public SugPoDtl getDtl(int  sugNum)
        {
            SugPoDtl s = new SugPoDtl();
            return s;
        }

        public string getDtl2(SugPoDtl sugPoDtl)
        {
           
            return "";
        }


        /// <summary>
        /// 传入建议编号，将建议数据冲E9复制到阿里云服务器
        /// </summary>
        /// <param name="sugNum"></param>
        /// <returns></returns>
        [WebMethod(Description = "传入建议编号，将建议数据冲E9复制到阿里云服务器")]
        public string getDtlAndAdd(int sugNum)
        {
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                log.Error("e10登入失败"+ex.Message, ex);
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug = null;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            Int32 sugnum = sugNum;//2476043;

            try
            {
                dsposug = posugBO.GetByID(sugnum);
            }
            catch (Exception ex)
            {

                Console.Write(ex);
            }
            


            try
            {
                if (dsposug != null && dsposug.Tables.Count > 0 && dsposug.Tables["SugPoDtl"] != null &&  dsposug.Tables["SugPoDtl"].Rows.Count > 0)
                {

                
                SugPoDtl s = new SugPoDtl();
                s.Company = dsposug.Tables["SugPoDtl"].Rows[0]["Company"].ToString();
                s.DueDate = Convert.ToDateTime(dsposug.SugPoDtl.Rows[0]["DueDate"]);
                s.XRelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["XRelQty"]);
                s.PurchasingFactor = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["PurchasingFactor"]);
                s.RelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["RelQty"]);
                s.JobNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["JobNum"]);
                s.AssemblySeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["AssemblySeq"]);
                s.SugType = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["SugType"]);
                s.JobSeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["JobSeq"]);
                s.WarehouseCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["WarehouseCode"]);

                s.PONUM = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["PONUM"]);
                s.POLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["POLine"]);
                s.OpCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["OpCode"]);
                s.FOB = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["FOB"]);
                s.ShipViaCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShipViaCode"]);
                s.TermsCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["TermsCode"]);
                s.BuyerID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["BuyerID"]);
                s.VendorNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["VendorNum"]);
                s.PurPoint = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PurPoint"]);
                s.LineDesc = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["LineDesc"]);

                s.IUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["IUM"]);
                s.UnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["UnitPrice"]);
                s.Taxable = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["Taxable"]);
                s.PUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PUM"]);
                s.CostPerCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["CostPerCode"]);
                s.PartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PartNum"]);
                s.VenPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["VenPartNum"].ToString().Length>0? dsposug.Tables["SugPoDtl"].Rows[0]["VenPartNum"] : s.PartNum);
                s.CommentText = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["CommentText"]);
                s.ClassID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ClassID"]);
                s.RevisionNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["RevisionNum"]);

                s.SugNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["SugNum"]);
                s.VendorID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["VendorID"]);
                s.Name = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Name"]);
                s.Buy = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["Buy"]);
                s.OrderByDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["OrderByDate"]);
                s.DocUnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["DocUnitPrice"]);
                s.LeadTime = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["LeadTime"]);
                s.ReqNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ReqNum"]);
                s.ReqLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ReqLine"]);
                s.Plant = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Plant"]);
                s.CreateRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CreateRFQ"]);
                s.SourceRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["SourceRFQ"]);
                s.PurchasingFactorDirection = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PurchasingFactorDirection"]);

                s.ShortChar01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar01"]);
                s.ShortChar02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar02"]);
                s.ShortChar03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar03"]);
                s.ShortChar04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar04"]);
                s.ShortChar05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar05"]);
                s.Number01 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number01"].ToString());
                s.Number02 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number02"].ToString());
                s.Number03 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number03"].ToString());
                s.Number04 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number04"].ToString());
                s.Number05 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number05"].ToString());
                s.Date01 = dsposug.Tables["SugPoDtl"].Rows[0]["Date01"].ToString().Length> 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date01"]): s.Date01;
                s.Date02 = dsposug.Tables["SugPoDtl"].Rows[0]["Date02"].ToString().Length> 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date02"]) : s.Date02;
                s.Date03 = dsposug.Tables["SugPoDtl"].Rows[0]["Date03"].ToString().Length> 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date03"]) : s.Date03;
                s.Date04 = dsposug.Tables["SugPoDtl"].Rows[0]["Date04"].ToString().Length> 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date04"]) : s.Date04;
                s.Date05 = dsposug.Tables["SugPoDtl"].Rows[0]["Date05"].ToString().Length> 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date05"]) : s.Date05;
                s.CheckBox01 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox01"]);
                s.CheckBox02 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox02"]);
                s.CheckBox03 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox03"]);
                s.CheckBox04 = false; //Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox04"]);
                s.CheckBox05 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox05"]);
                s.Character01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character01"]);
                s.Character02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character02"]);
                s.Character03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character03"]);
                s.Character04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character04"]);
                s.Character05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character05"]);
                s.Character06 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character06"]);
                s.Character07 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character07"]);
                s.Character08 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character08"]);
                s.Character09 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character09"]);
                s.Character10 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character10"]);

                s.OrderNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderNum"]);
                s.OrderLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderLine"]);
                s.OrderRelNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderRelNum"]);
                s.DropShip = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["DropShip"]);
                s.MfgNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["MfgNum"]);
                s.MfgPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["MfgPartNum"]);
                s.ShipToNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShipToNum"]);
                s.SoldToNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["SoldToNum"]);
                s.BitFlag = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["BitFlag"]);
                s.ShpConNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ShpConNum"]);
                s.ShipToCustNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ShipToCustNum"]);
                s.ComplianceMsg = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ComplianceMsg"]);
                s.PORelOpen = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["PORelOpen"]);
                s.SugReason = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["SugReason"]);
                s.ABCCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ABCCode"]);

                SCMDataProvider.DB.SugPoDtl.InsertOnSubmit(s);
                SCMDataProvider.DB.SubmitChanges();

                }
                else
                {
                    log.Error("在e9中未找到，建议编号,sugNum="+sugNum);
                    //
                     return "no data";
                }
            }
            catch (Exception e)
            {
                log.Error(sugNum.ToString()+"没有创建成功，" +e.Message, e);
                return "unsuccess";
               
            }
            finally
            {
                objSess.Dispose();
            }
            return "success";
        }

        /// <summary>
        /// 传入建议编号，将建议数据冲E9复制到阿里云服务器
        /// </summary>
        /// <param name="sugNum"></param>
        /// <returns></returns>
        [WebMethod(Description = "传入建议编号，将建议数据冲E9复制到阿里云服务器")]
        public string getDtlAndAdd2a(int sugNum,int reqnum)
        {
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception ex)
            {
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                log.Error("e10登入失败" + ex.Message, ex);
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug = null;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            Int32 sugnum = sugNum;//2476043;

            try
            {
                dsposug = posugBO.GetByID(sugnum);
            }
            catch (Exception ex)
            {

                Console.Write(ex);
            }



            try
            {
                if (dsposug != null && dsposug.Tables.Count > 0 && dsposug.Tables["SugPoDtl"] != null && dsposug.Tables["SugPoDtl"].Rows.Count > 0)
                {
                    int rowindex = 0;
                    int rowlenth = dsposug.Tables["SugPoDtl"].Rows.Count;
                    for (int i = 0; i < rowlenth; i++)
                    {
                        if (reqnum == (dsposug.SugPoDtl.Rows[i]["ReqNum"] == null ? 0: Convert.ToInt32(dsposug.SugPoDtl.Rows[i]["ReqNum"])))
                        {
                            rowindex = i;
                        }
                       
                    }
                    

                    SugPoDtl s = new SugPoDtl();
                    s.Company = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Company"].ToString();
                    s.DueDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DueDate"]);
                    s.XRelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["XRelQty"]);
                    s.PurchasingFactor = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurchasingFactor"]);
                    s.RelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["RelQty"]);
                    s.JobNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["JobNum"]);
                    s.AssemblySeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["AssemblySeq"]);
                    s.SugType = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugType"]);
                    s.JobSeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["JobSeq"]);
                    s.WarehouseCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["WarehouseCode"]);

                    s.PONUM = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PONUM"]);
                    s.POLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["POLine"]);
                    s.OpCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OpCode"]);
                    s.FOB = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["FOB"]);
                    s.ShipViaCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipViaCode"]);
                    s.TermsCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["TermsCode"]);
                    s.BuyerID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["BuyerID"]);
                    s.VendorNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VendorNum"]);
                    s.PurPoint = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurPoint"]);
                    s.LineDesc = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["LineDesc"]);

                    s.IUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["IUM"]);
                    s.UnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["UnitPrice"]);
                    s.Taxable = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Taxable"]);
                    s.PUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PUM"]);
                    s.CostPerCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CostPerCode"]);
                    s.PartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PartNum"]);
                    s.VenPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VenPartNum"].ToString().Length > 0 ? dsposug.Tables["SugPoDtl"].Rows[rowindex]["VenPartNum"] : s.PartNum);
                    s.CommentText = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CommentText"]);
                    s.ClassID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ClassID"]);
                    s.RevisionNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["RevisionNum"]);

                    s.SugNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugNum"]);
                    s.VendorID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VendorID"]);
                    s.Name = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Name"]);
                    s.Buy = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Buy"]);
                    s.OrderByDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderByDate"]);
                    s.DocUnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DocUnitPrice"]);
                    s.LeadTime = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["LeadTime"]);
                    s.ReqNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ReqNum"]);
                    s.ReqLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ReqLine"]);
                    s.Plant = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Plant"]);
                    s.CreateRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CreateRFQ"]);
                    s.SourceRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SourceRFQ"]);
                    s.PurchasingFactorDirection = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurchasingFactorDirection"]);

                    s.ShortChar01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar01"]);
                    s.ShortChar02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar02"]);
                    s.ShortChar03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar03"]);
                    s.ShortChar04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar04"]);
                    s.ShortChar05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar05"]);
                    s.Number01 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number01"].ToString());
                    s.Number02 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number02"].ToString());
                    s.Number03 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number03"].ToString());
                    s.Number04 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number04"].ToString());
                    s.Number05 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number05"].ToString());
                    s.Date01 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date01"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date01"]) : s.Date01;
                    s.Date02 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date02"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date02"]) : s.Date02;
                    s.Date03 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date03"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date03"]) : s.Date03;
                    s.Date04 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date04"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date04"]) : s.Date04;
                    s.Date05 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date05"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date05"]) : s.Date05;
                    s.CheckBox01 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox01"]);
                    s.CheckBox02 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox02"]);
                    s.CheckBox03 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox03"]);
                    s.CheckBox04 = false; //Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox04"]);
                    s.CheckBox05 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox05"]);
                    s.Character01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character01"]);
                    s.Character02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character02"]);
                    s.Character03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character03"]);
                    s.Character04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character04"]);
                    s.Character05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character05"]);
                    s.Character06 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character06"]);
                    s.Character07 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character07"]);
                    s.Character08 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character08"]);
                    s.Character09 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character09"]);
                    s.Character10 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character10"]);

                    s.OrderNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderNum"]);
                    s.OrderLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderLine"]);
                    s.OrderRelNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderRelNum"]);
                    s.DropShip = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DropShip"]);
                    s.MfgNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["MfgNum"]);
                    s.MfgPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["MfgPartNum"]);
                    s.ShipToNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipToNum"]);
                    s.SoldToNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SoldToNum"]);
                    s.BitFlag = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["BitFlag"]);
                    s.ShpConNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShpConNum"]);
                    s.ShipToCustNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipToCustNum"]);
                    s.ComplianceMsg = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ComplianceMsg"]);
                    s.PORelOpen = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PORelOpen"]);
                    s.SugReason = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugReason"]);
                    s.ABCCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ABCCode"]);

                    SCMDataProvider.DB.SugPoDtl.InsertOnSubmit(s);
                    SCMDataProvider.DB.SubmitChanges();

                }
                else
                {
                    log.Error("在e9中未找到，建议编号,sugNum=" + sugNum);
                    //
                    return "no data";
                }
            }
            catch (Exception e)
            {
                log.Error(sugNum.ToString() + "没有创建成功，" + e.Message, e);
                return "unsuccess";

            }
            finally
            {
                objSess.Dispose();
            }
            return "success";
        }

        /// <summary>
        /// 传入建议编号，将建议数据E9更新到阿里云服务器
        /// </summary>
        /// <param name="sugNum"></param>
        /// <returns></returns>
        [WebMethod(Description = "传入建议编号，将建议数据E9更新到阿里云服务器")]
        public string getDtlAndUpdate(int sugNum)
        {
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception)
            {
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            Int32 sugnum = sugNum;//2476043;
            dsposug = posugBO.GetByID(sugnum);


            try
            {
                if (dsposug.Tables.Count > 0 && dsposug.Tables["SugPoDtl"] != null && dsposug.Tables["SugPoDtl"].Rows.Count > 0)
                {
                    IEnumerable rows = SCMDataProvider.GetSugPoDtl(sugnum);
                    IEnumerator enumerator = rows.GetEnumerator();
                    SugPoDtl s = null;
                    if (enumerator.MoveNext())
                    {
                        s = (SugPoDtl)enumerator.Current;
                    }
                    else
                    {
                        string message = string.Format("在供应链系统中未找到建议{0}，未更新成功。", sugnum);
                        log.Error(message);
                        return message;
                    }

                    try
                    {

                        //SugPoDtl s = new SugPoDtl();
                        s.Company = dsposug.Tables["SugPoDtl"].Rows[0]["Company"].ToString();
                        s.DueDate = Convert.ToDateTime(dsposug.SugPoDtl.Rows[0]["DueDate"]);
                        s.XRelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["XRelQty"]);
                        s.PurchasingFactor = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["PurchasingFactor"]);
                        s.RelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["RelQty"]);
                        s.JobNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["JobNum"]);
                        s.AssemblySeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["AssemblySeq"]);
                        s.SugType = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["SugType"]);
                        s.JobSeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["JobSeq"]);
                        s.WarehouseCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["WarehouseCode"]);

                        s.PONUM = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["PONUM"]);
                        s.POLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["POLine"]);
                        s.OpCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["OpCode"]);
                        s.FOB = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["FOB"]);
                        s.ShipViaCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShipViaCode"]);
                        s.TermsCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["TermsCode"]);
                        s.BuyerID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["BuyerID"]);
                        s.VendorNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["VendorNum"]);
                        s.PurPoint = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PurPoint"]);
                        s.LineDesc = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["LineDesc"]);

                        s.IUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["IUM"]);
                        s.UnitPrice = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["UnitPrice"]);
                        s.Taxable = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["Taxable"]);
                        s.PUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PUM"]);
                        s.CostPerCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["CostPerCode"]);
                        s.PartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PartNum"]);
                        s.VenPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["VenPartNum"].ToString().Length > 0 ? dsposug.Tables["SugPoDtl"].Rows[0]["VenPartNum"] : s.PartNum);
                        s.CommentText = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["CommentText"]);
                        s.ClassID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ClassID"]);
                        s.RevisionNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["RevisionNum"]);

                        s.SugNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["SugNum"]);
                        s.VendorID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["VendorID"]);
                        s.Name = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Name"]);
                        s.Buy = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["Buy"]);
                        s.OrderByDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["OrderByDate"]);
                        s.DocUnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["DocUnitPrice"]);
                        s.LeadTime = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["LeadTime"]);
                        s.ReqNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ReqNum"]);
                        s.ReqLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ReqLine"]);
                        s.Plant = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Plant"]);
                        s.CreateRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CreateRFQ"]);
                        s.SourceRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["SourceRFQ"]);
                        s.PurchasingFactorDirection = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["PurchasingFactorDirection"]);

                        s.ShortChar01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar01"]);
                        s.ShortChar02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar02"]);
                        s.ShortChar03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar03"]);
                        s.ShortChar04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar04"]);
                        s.ShortChar05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShortChar05"]);
                        s.Number01 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number01"].ToString());
                        s.Number02 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number02"].ToString());
                        s.Number03 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number03"].ToString());
                        s.Number04 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number04"].ToString());
                        s.Number05 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[0]["Number05"].ToString());
                        s.Date01 = dsposug.Tables["SugPoDtl"].Rows[0]["Date01"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date01"]) : s.Date01;
                        s.Date02 = dsposug.Tables["SugPoDtl"].Rows[0]["Date02"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date02"]) : s.Date02;
                        s.Date03 = dsposug.Tables["SugPoDtl"].Rows[0]["Date03"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date03"]) : s.Date03;
                        s.Date04 = dsposug.Tables["SugPoDtl"].Rows[0]["Date04"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date04"]) : s.Date04;
                        s.Date05 = dsposug.Tables["SugPoDtl"].Rows[0]["Date05"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[0]["Date05"]) : s.Date05;
                        s.CheckBox01 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox01"]);
                        s.CheckBox02 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox02"]);
                        s.CheckBox03 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox03"]);
                        s.CheckBox04 = false;//Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox04"]);
                        s.CheckBox05 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["CheckBox05"]);
                        s.Character01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character01"]);
                        s.Character02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character02"]);
                        s.Character03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character03"]);
                        s.Character04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character04"]);
                        s.Character05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character05"]);
                        s.Character06 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character06"]);
                        s.Character07 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character07"]);
                        s.Character08 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character08"]);
                        s.Character09 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character09"]);
                        s.Character10 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["Character10"]);

                        s.OrderNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderNum"]);
                        s.OrderLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderLine"]);
                        s.OrderRelNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["OrderRelNum"]);
                        s.DropShip = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["DropShip"]);
                        s.MfgNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["MfgNum"]);
                        s.MfgPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["MfgPartNum"]);
                        s.ShipToNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ShipToNum"]);
                        s.SoldToNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["SoldToNum"]);
                        s.BitFlag = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["BitFlag"]);
                        s.ShpConNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ShpConNum"]);
                        s.ShipToCustNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[0]["ShipToCustNum"]);
                        s.ComplianceMsg = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ComplianceMsg"]);
                        s.PORelOpen = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[0]["PORelOpen"]);
                        s.SugReason = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["SugReason"]);
                        s.ABCCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[0]["ABCCode"]);

                        //SCMDataProvider.DB.SugPoDtl.Attach(s);
                        //SCMDataProvider.DB.Refresh(RefreshMode.KeepCurrentValues, s);
                        //SCMDataProvider.DB.SubmitChanges(ConflictMode.ContinueOnConflict);
                        SCMDataProvider.DB.SubmitChanges();
                    }
                    catch (Exception e)
                    {
                        log.Error(sugNum.ToString() + "没有更新成功，" + e.Message, e);
                        return "unsuccess";
                    }

                }
                else
                {
                    log.Error("在e9中未找到，建议编号,sugNum=" + sugNum);
                    return "no data";
                }
            }
            catch (Exception e)
            {
                log.Error(sugNum.ToString() + "没有更新成功，" + e.Message, e);
                return "unsuccess";

            }
            finally
            {
                objSess.Dispose();   
            }
            return "success";
        }

        /// <summary>
        /// 传入建议编号，将建议数据E9更新到阿里云服务器
        /// </summary>
        /// <param name="sugNum"></param>
        /// <returns></returns>
        [WebMethod(Description = "传入建议编号，将建议数据E9更新到阿里云服务器")]
        public string getDtlAndUpdate2a(int sugNum,int reqnum)
        {
            string userID = System.Configuration.ConfigurationManager.AppSettings["EpiUserID"];
            string passWord = System.Configuration.ConfigurationManager.AppSettings["EpiPassword"];
            string sasUrl = System.Configuration.ConfigurationManager.AppSettings["EpiAppUrl"];

            Epicor.Mfg.Core.Session objSess;
            try
            {
                objSess = new Epicor.Mfg.Core.Session(userID, passWord, sasUrl, Epicor.Mfg.Core.Session.LicenseType.DataCollection);
                objSess.CompanyID = "001";

            }
            catch (Exception)
            {
                //System.Windows.Forms.MessageBox.Show(ex.ToString());
                objSess = null;
            }
            Epicor.Mfg.BO.POSugg posugBO;
            Epicor.Mfg.BO.POSuggDataSet dsposug;
            posugBO = new Epicor.Mfg.BO.POSugg(objSess.ConnectionPool);
            Int32 sugnum = sugNum;//2476043;
           


            try
            {
                //log.Error(string.Format("建议号：{0} 请购单号：{1}",sugnum, reqnum));
                dsposug = posugBO.GetByID(sugnum);
                if (dsposug.Tables.Count > 0 && dsposug.Tables["SugPoDtl"] != null && dsposug.Tables["SugPoDtl"].Rows.Count > 0)
                {
                    IEnumerable rows = SCMDataProvider.GetSugPoDtl(sugnum,reqnum);
                    IEnumerator enumerator = rows.GetEnumerator();
                    SugPoDtl s = null;
                    if (enumerator.MoveNext())
                    {
                        s = (SugPoDtl)enumerator.Current;
                    }
                    else
                    {
                        string message = string.Format("在供应链系统中未找到建议{0}，未更新成功。", sugnum);
                        log.Error(message);
                        return message;
                    }

                    int rowindex = 0;
                    int rowlenth = dsposug.Tables["SugPoDtl"].Rows.Count;
                    for (int i = 0; i < rowlenth; i++)
                    {
                        if (reqnum == (dsposug.SugPoDtl.Rows[i]["ReqNum"] == null ? 0 : Convert.ToInt32(dsposug.SugPoDtl.Rows[i]["ReqNum"])))
                        {
                            rowindex = i;
                        }

                    }
                    try
                    {
                        //log.Error(sugNum.ToString() + "更新jiage1，" + s.UnitPrice.ToString());
                        //SugPoDtl s = new SugPoDtl();
                        s.Company = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Company"].ToString();
                        s.DueDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DueDate"]);
                        s.XRelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["XRelQty"]);
                        s.PurchasingFactor = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurchasingFactor"]);
                        s.RelQty = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["RelQty"]);
                        s.JobNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["JobNum"]);
                        s.AssemblySeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["AssemblySeq"]);
                        s.SugType = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugType"]);
                        s.JobSeq = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["JobSeq"]);
                        s.WarehouseCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["WarehouseCode"]);

                        s.PONUM = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PONUM"]);
                        s.POLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["POLine"]);
                        s.OpCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OpCode"]);
                        s.FOB = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["FOB"]);
                        s.ShipViaCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipViaCode"]);
                        s.TermsCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["TermsCode"]);
                        s.BuyerID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["BuyerID"]);
                        s.VendorNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VendorNum"]);
                        s.PurPoint = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurPoint"]);
                        s.LineDesc = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["LineDesc"]);

                        s.IUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["IUM"]);
                        s.UnitPrice = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["UnitPrice"]);
                        s.Taxable = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Taxable"]);
                        s.PUM = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PUM"]);
                        s.CostPerCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CostPerCode"]);
                        s.PartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PartNum"]);
                        s.VenPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VenPartNum"].ToString().Length > 0 ? dsposug.Tables["SugPoDtl"].Rows[rowindex]["VenPartNum"] : s.PartNum);
                        s.CommentText = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CommentText"]);
                        s.ClassID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ClassID"]);
                        s.RevisionNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["RevisionNum"]);

                        s.SugNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugNum"]);
                        s.VendorID = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["VendorID"]);
                        s.Name = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Name"]);
                        s.Buy = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Buy"]);
                        s.OrderByDate = Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderByDate"]);
                        s.DocUnitPrice = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DocUnitPrice"]);
                        s.LeadTime = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["LeadTime"]);
                        s.ReqNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ReqNum"]);
                        s.ReqLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ReqLine"]);
                        s.Plant = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Plant"]);
                        s.CreateRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CreateRFQ"]);
                        s.SourceRFQ = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SourceRFQ"]);
                        s.PurchasingFactorDirection = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PurchasingFactorDirection"]);

                        s.ShortChar01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar01"]);
                        s.ShortChar02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar02"]);
                        s.ShortChar03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar03"]);
                        s.ShortChar04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar04"]);
                        s.ShortChar05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShortChar05"]);
                        s.Number01 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number01"].ToString());
                        s.Number02 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number02"].ToString());
                        s.Number03 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number03"].ToString());
                        s.Number04 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number04"].ToString());
                        s.Number05 = Convert.ToDecimal(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Number05"].ToString());
                        s.Date01 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date01"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date01"]) : s.Date01;
                        s.Date02 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date02"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date02"]) : s.Date02;
                        s.Date03 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date03"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date03"]) : s.Date03;
                        s.Date04 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date04"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date04"]) : s.Date04;
                        s.Date05 = dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date05"].ToString().Length > 0 ? Convert.ToDateTime(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Date05"]) : s.Date05;
                        s.CheckBox01 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox01"]);
                        s.CheckBox02 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox02"]);
                        s.CheckBox03 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox03"]);
                        s.CheckBox04 = false;//Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox04"]);
                        s.CheckBox05 = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["CheckBox05"]);
                        s.Character01 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character01"]);
                        s.Character02 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character02"]);
                        s.Character03 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character03"]);
                        s.Character04 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character04"]);
                        s.Character05 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character05"]);
                        s.Character06 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character06"]);
                        s.Character07 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character07"]);
                        s.Character08 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character08"]);
                        s.Character09 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character09"]);
                        s.Character10 = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["Character10"]);

                        s.OrderNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderNum"]);
                        s.OrderLine = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderLine"]);
                        s.OrderRelNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["OrderRelNum"]);
                        s.DropShip = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["DropShip"]);
                        s.MfgNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["MfgNum"]);
                        s.MfgPartNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["MfgPartNum"]);
                        s.ShipToNum = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipToNum"]);
                        s.SoldToNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SoldToNum"]);
                        s.BitFlag = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["BitFlag"]);
                        s.ShpConNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShpConNum"]);
                        s.ShipToCustNum = Convert.ToInt32(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ShipToCustNum"]);
                        s.ComplianceMsg = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ComplianceMsg"]);
                        s.PORelOpen = Convert.ToBoolean(dsposug.Tables["SugPoDtl"].Rows[rowindex]["PORelOpen"]);
                        s.SugReason = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["SugReason"]);
                        s.ABCCode = Convert.ToString(dsposug.Tables["SugPoDtl"].Rows[rowindex]["ABCCode"]);

                        //SCMDataProvider.DB.SugPoDtl.Attach(s);
                        //SCMDataProvider.DB.Refresh(RefreshMode.KeepCurrentValues, s);
                        //SCMDataProvider.DB.SubmitChanges(ConflictMode.ContinueOnConflict);
                        //log.Error(sugNum.ToString() + "更新jiage，" + s.UnitPrice.ToString());
                        SCMDataProvider.DB.SubmitChanges();
                    }
                    catch (Exception e)
                    {

                        log.Error(sugNum.ToString() + "没有更新成功，" + e.Message, e);
                        return "unsuccess";
                    }
                

                }
                else
                {
                    log.Error("在e9中未找到，建议编号,sugNum=" + sugNum);
                    return "no data";
                }
            }
            catch (Exception e)
            {
                log.Error(sugNum.ToString() + "没有更新成功，" + e.Message, e);
                return "unsuccess";

            }
            finally
            {
                objSess.Dispose();
            }
            return "success";
        }
    }
}
